﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;

namespace PickEmHats
{
    public class UserFactory : HatsFactoryBase
    {
        public User GetUserById(int id)
        {
            User u = new User();
            InstantiateDBConnection();
            sqlCmd.CommandText = @"";
            sqlCmd.Parameters.AddWithValue("?id", id);
            using (sqlConn)
            {
                try
                {
                    sqlConn.Open();
                    sqlRdr = sqlCmd.ExecuteReader();
                    while (sqlRdr.Read())
                    {
                        u.Id = Convert.ToInt32(sqlRdr["id"]);
                        u.First_name = sqlRdr["first_name"].ToString();
                        u.Last_name = sqlRdr["last_name"].ToString();
                    }
                }
                catch (MySqlException e)
                {
                    throw e;
                }
            }
            return u;
        }

        public List<User> GetUserList()
        {
            List<User> lstUser = new List<User>();
            InstantiateDBConnection();
            sqlCmd.CommandText = @"";
            using (sqlConn)
            {
                try
                {
                    sqlConn.Open();
                    sqlRdr = sqlCmd.ExecuteReader();
                    while (sqlRdr.Read())
                    {
                        lstUser.Add(new User(Convert.ToInt32(sqlRdr["id"]),
                                            sqlRdr["first_name"].ToString(),
                                            sqlRdr["last_name"].ToString()));
                    }
                }
                catch (MySqlException e)
                {
                    throw e;
                }
            }
            return lstUser;
        }

    }
}